/**
 * 
 */
package com.fox.webapp.web.admin;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fox.webapp.domain.Account;
import com.fox.webapp.domain.Person;
import com.fox.webapp.service.IAccountService;
import com.fox.webapp.service.IPersonService;
import com.fox.webapp.service.impl.AccountService;
import com.fox.webapp.utils.DataTablesRequest;
import com.fox.webapp.utils.DataTablesResponse;

/**
 * @author yockii
 *
 */
@Controller
@RequestMapping("admin/person")
public class PersonAdminController {
	private static final Logger logger = LoggerFactory.getLogger(PersonAdminController.class);
	@Autowired
	private IPersonService personService;
	@Autowired
	private IAccountService accountService;
	
	@RequestMapping(value = "show", method = RequestMethod.GET)
	public String showPerson(){
		return "admin/person/show";
	}
	
	@RequestMapping(value = "p", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponse<Person> getPerson(Person person, DataTablesRequest req, DataTablesResponse<Person> res){
		int totalPersonNumber = personService.countPersonNumWithCondition(person);
		if(totalPersonNumber > 0){
			int page = req.getiDisplayStart() / req.getiDisplayLength();
			res.setAaData(personService.getPersonWithCondition(person, page, req.getiDisplayLength()).getContent());
		} else {
			res.setAaData(new ArrayList<Person>());
		}
		res.setiTotalRecords(totalPersonNumber);
		res.setiTotalDisplayRecords(totalPersonNumber);
		return res;
	}
	
	@RequestMapping(value = "account", method = RequestMethod.GET)
	public String showAccount(){
		return "admin/person/account";
	}
	
	@RequestMapping(value = "account", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponse<Account> getAccount(Account account, DataTablesRequest req, DataTablesResponse<Account> res){
		int page = req.getiDisplayStart() / req.getiDisplayLength();
		Page<Account> p = accountService.getAccountWithCondition(account, page, req.getiDisplayLength());
		res.setAaData(p.getContent());
		res.setiTotalDisplayRecords((int) p.getTotalElements());
		res.setiTotalRecords((int) p.getTotalElements());
		return res;
	}
	
	@RequestMapping(value = "ipProblem", method = RequestMethod.GET)
	public String ipProblemPersonPage(){
		return "admin/person/ipProblem";
	}
}
